package com.xiaobug.demo.okgo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.ljb.commlibrary.http.HttpParams;
import com.ljb.commlibrary.http.HttpUtils;
import com.ljb.commlibrary.okgo.HttpCallBack;
import com.ljb.commlibrary.utils.L;
import com.xiaobug.baselibrary.base.BaseActivity;
import com.xiaobug.demo.R;
import com.xiaobug.demo.TestBean;
import com.xiaobug.demo.databinding.OkgoActivityTestBinding;

/**
 * Created by ljb on 2018/5/4.
 */

public class OkgoTestActivity extends BaseActivity<OkgoActivityTestBinding> {
    public class Presenter {
        public void onClick1(View view) {
            testGet();
        }

        public void onClick2(View view) {
            testPost();
        }

        public void onClick3(View view) {
            testCancle();
        }

    }

    String url = "http://app.shukuang.cn/ZHSW_APP_INTERFACE/system/factory/listByUserId.ajax";

    public static void actionStart(Context context) {
        context.startActivity(new Intent(context, OkgoTestActivity.class));
    }

    @Override
    protected int getRootID() {
        return R.layout.okgo_activity_test;
    }


    @Override
    protected void initData(Bundle savedInstanceState, OkgoActivityTestBinding binding) {
        binding.setP(new Presenter());
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.fl_container, new OkgoTestFragment());
        ft.commit();
    }

    private void testCancle() {
        testGet();
        HttpUtils.getInstance().cancle(url);

    }

    /**
     * 测试get方法
     */
    private void testGet() {
        HttpParams httpParams = new HttpParams();
        httpParams.put("factoryId", "0");
        httpParams.put("userId", "89");

        HttpUtils.getInstance().get(url,  httpParams, TestBean.class, new HttpCallBack<TestBean>() {
            @Override
            public void onSucess(TestBean testBean) {
                L.e(new Gson().toJson(testBean));
            }

            @Override
            public void onError() {
                L.e("请求失败");
            }
        });
    }

    /**
     * 测试post方法
     */
    private void testPost() {
        HttpParams httpParams = new HttpParams();
        httpParams.put("factoryId", "0");
        httpParams.put("userId", "89");

        HttpUtils.getInstance().post(url, httpParams, TestBean.class, new HttpCallBack<TestBean>() {
            @Override
            public void onSucess(TestBean testBean) {
                L.e(JSON.toJSONString(testBean));
            }

            @Override
            public void onError() {
                L.e("请求失败");
            }
        });
    }
}
